package com.wisdom.benefit.jufa.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.my.UserPassagewayListBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class DetailsBalanceAdapter extends BaseAdapter {
    private ArrayList<UserPassagewayListBean> datas;
    private Context context;

    public DetailsBalanceAdapter(ArrayList<UserPassagewayListBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DetailsBalanceAdapter.ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.details_balance_item, parent, false);
            holder = new DetailsBalanceAdapter.ViewHolder();
            holder.timeTv = (TextView) convertView.findViewById(R.id.balance_time_tv);
            holder.balanceBeforeChangeTv = (TextView) convertView.findViewById(R.id.balance_balance_before_change_tv);
            holder.balanceBehindChangeTv = (TextView) convertView.findViewById(R.id.balance_balance_behind_change_tv);
            holder.balanceResultTv = (TextView) convertView.findViewById(R.id.balance_balance_result_tv);
            holder.variableBalanceTv = (TextView) convertView.findViewById(R.id.balance_variable_balance_tv);
            convertView.setTag(holder);
        } else {
            holder = (DetailsBalanceAdapter.ViewHolder) convertView.getTag();
        }
        holder.timeTv.setText(datas.get(position).getGmDate());
        holder.balanceResultTv.setText(String.valueOf(datas.get(position).getChangeReason()));
        holder.balanceBeforeChangeTv.setText(String.valueOf(datas.get(position).getQuota()));
        holder.variableBalanceTv.setText(String.valueOf(datas.get(position).getActualQuota() -
                datas.get(position).getQuota()));
        holder.balanceBehindChangeTv.setText(String.valueOf(datas.get(position).getActualQuota()));
        return convertView;
    }

    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    static class ViewHolder {
        TextView timeTv, variableBalanceTv,
                balanceBeforeChangeTv, balanceBehindChangeTv, balanceResultTv;
    }
}
